﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DTO;
using BUS;

namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for LapPhieuThuUserControl.xaml
    /// </summary>
    public partial class LapPhieuThuUserControl : UserControl
    {
        private Section_DTO Section { get; set; }
        private MainWindow _parent;
        public LapPhieuThuUserControl(Section_DTO sect, MainWindow par)
        {
            _parent = par;
            this.Section = sect;
            InitializeComponent();
        }

        #region Phuong thu xu ly
        private void phieuThuMoi_Click(object sender, RoutedEventArgs e)
        {
            LapPhieuMoi();
            maKhachHang.Focus();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            PhatSinhMaPhieuThu();
            ngayLap.SelectedDate = System.DateTime.Now;
        }

        private void xemTruoc_Click(object sender, RoutedEventArgs e)
        {

        }

        private void lapPhieu_Click(object sender, RoutedEventArgs e)
        {
            ThemPhieuThuTien();
        }

        private void thoat_Click(object sender, RoutedEventArgs e)
        {
            _parent.ChangeUI(new WelcomeUserControl(Section));
        }

        #endregion

        #region Ham ho tro
        private void PhatSinhMaPhieuThu()
        {
            int maLonNhat = PhieuThuTien_BUS.PHIEUTHUTIEN.LayMaPhieuThuLonNhat();

            if (maLonNhat != -999)
                maPhieuThu.Text = (maLonNhat + 1).ToString();
            else
                maPhieuThu.Text = Convert.ToString(1);
        }

        private void DienThongTinKhachHang()
        {
            try
            {
                if (maKhachHang.Text == String.Empty)
                {
                    XoaThongTinKhachHang();
                    return;
                }
                int ms = Int32.Parse(maKhachHang.Text);
                KhachHang_DTO kh = KhachHang_BUS.KHACHHANG.LayKhachHang(ms);

                if (kh == null)
                {
                    // thong bao loi
                    MessageBox.Show("Ma khach hang khong hop le.");
                    maKhachHang.Focus();
                    return;
                }

                hoTen.Text = kh.HoTen;
                diaChi.Text = kh.DiaChi;
                eMail.Text = kh.Email;
                dienThoai.Text = kh.DienThoai;
                tienNo.Text = kh.TienNo.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("Ma khach hang phai la mot so");
                XoaThongTinKhachHang();
                maKhachHang.Focus();
                return;
            }
        }

        private void LapPhieuMoi()
        {
            PhatSinhMaPhieuThu();
            maKhachHang.Text = "";
            maKhachHang.Focus();
            hoTen.Text = "";
            diaChi.Text = "";
            eMail.Text = "";
            tienNo.Text = "";
            dienThoai.Text = "";
        }

        private void ThemPhieuThuTien()
        {
            if (maKhachHang.Text == "")
            {
                MessageBox.Show("Mã Khách Hàng không được rỗng!", "Cảnh Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                maKhachHang.Focus();
                return;
            }

            if (soTienThu.Text == "")
            {
                MessageBox.Show("Số tiền thu không được rỗng!", "Cảnh Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                soTienThu.Focus();
                return;
            }

            bool a4 = ThamSo_BUS.THAMSO.ApDung_QD_TienThuKhongVuotTienNo();

            if (a4 == true)
            {
                if (float.Parse(soTienThu.Text) > float.Parse(tienNo.Text))
                {
                    MessageBox.Show("Số tiền thu không được vượt số tiền nợ!", "Cảnh Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                    soTienThu.Focus();
                    return;
                }
            }

            PhieuThuTien_BUS.PHIEUTHUTIEN.ThemPhieuThuTien(new PhieuThuTien_DTO(Int32.Parse(maKhachHang.Text), ngayLap.Text, float.Parse(soTienThu.Text)));
            
            float no = float.Parse(tienNo.Text) - float.Parse(soTienThu.Text);
            KhachHang_DTO k = new KhachHang_DTO(Int32.Parse(maKhachHang.Text), hoTen.Text, diaChi.Text, eMail.Text, dienThoai.Text, no);
            KhachHang_BUS.KHACHHANG.SuaKhachHang(k);

            MessageBox.Show("Lập Phiếu thu thành công.!", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            LapPhieuMoi();
        }
        #endregion

        private void maKhachHang_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            DienThongTinKhachHang();
            CapNhatLapPhieuButton();
        }

        private void XoaThongTinKhachHang()
        {
            maKhachHang.Text = String.Empty;
            hoTen.Text = String.Empty;
            tienNo.Text = String.Empty;
            diaChi.Text = String.Empty;
            eMail.Text = String.Empty;
            dienThoai.Text = String.Empty;
        }

        private void soTienThu_GotFocus(object sender, RoutedEventArgs e)
        {
            soTienThu.SelectAll();
        }

        private void soTienThu_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            try
            {
                if (soTienThu.Text == String.Empty || tienNo.Text == String.Empty)
                    return;

                float tienThu = float.Parse(soTienThu.Text);
                float No = float.Parse(tienNo.Text);
                bool apdungQD = ThamSo_BUS.THAMSO.ApDung_QD_TienThuKhongVuotTienNo();
                if (apdungQD)
                {
                    if (tienThu > No)
                    {
                        MessageBox.Show("Tien thu khong vuot qua tien no");
                        soTienThu.Focus();
                        return;
                    }
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("Tien thu phai la mot so.");
                soTienThu.Focus();
            }
            finally
            {
                CapNhatLapPhieuButton();
                lapPhieu.Focus();
            }
        }

        private void CapNhatLapPhieuButton()
        {
            if (maKhachHang.Text == String.Empty || soTienThu.Text == String.Empty)
                lapPhieu.IsEnabled = false;
            else
                lapPhieu.IsEnabled = true;
        }
    }
}
